<template>
  <div class="userList">
    <el-row :gutter="20" class="search">
      <el-col
        :span="8"
      ><span>人员搜索:</span>
        <el-input v-model="input" placeholder="请输入人员名称" />
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="searchBtn"
        >查询</el-button>
      </el-col>
    </el-row>
    <div class="table">
      <el-button
        icon="el-icon-circle-plus-outline"
        size="mini"
        @click="addBtn"
      >新建</el-button>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column
          type="index"
          :index="indexMethod"
          label="序号"
          width="80"
        />
        <el-table-column prop="userName" label="人员名称" width="180" />
        <el-table-column prop="regionName" label="归属区域" width="300" />
        <el-table-column prop="role.roleName" label="角色" width="300" />
        <el-table-column prop="mobile" label="联系电话" width="300" />
        <el-table-column prop="role" label="操作">
          <template slot-scope="scope">
            <span
              style="margin-right: 10px; color: blue"
              @click="edit(scope.row)"
            >修改</span>
            <span style="color: #ff5a5a" @click="del(scope.row)">删除</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="footer">
        <div>
          <span>共{{ totalCount }}条记录&nbsp;&nbsp;第{{ pageIndex }}/{{
            totalPage
          }}页</span>
        </div>
        <el-pagination
          layout="prev, next"
          :total="totalCount"
          :page-size="pageSize"
          prev-text="上一页"
          next-text="下一页"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <add ref="child" :form="form" :add="add" @reset="gender" />
  </div>
</template>

<script>
import { list, DEL } from '@/api/renyuan'
import add from './components/renyuan-add.vue'
export default {
  name: 'RenYuanList',
  components: {
    add
  },
  data() {
    return {
      add: '',
      input: null,
      tableData: [],
      pageIndex: 1,
      pageSize: 10,
      totalCount: null,
      totalPage: null,
      form: {
        id: null,
        userName: null,
        roleId: null,
        mobile: null,
        regionId: null,
        regionName: null,
        status: false,
        image: null
      }
    }
  },
  watch: {
    input(val) {
      if (val === '') {
        this.gender()
      }
    }
  },
  created() {
    this.gender()
  },
  methods: {
    async gender() {
      const data = await list({
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
        userName: this.input
      })
      this.tableData = data.currentPageRecords
      this.totalCount = +data.totalCount
      this.totalPage = +data.totalPage
    },
    searchBtn() {
      this.gender()
    },
    async del(row) {
      await DEL(row.id)
      this.gender()
    },

    addBtn() {
      this.add = 'add'
      this.$refs.child.dialogH()
    },
    edit(row) {
      this.add = 'edit'
      this.form.id = row.id
      this.form.userName = row.userName
      this.form.roleId = row.roleId
      this.form.mobile = row.mobile
      this.form.regionId = row.regionId
      this.form.regionName = row.regionName
      this.form.status = row.status
      this.form.image = row.image
      console.log(this.form)
      this.$refs.child.dialogH()
    },

    handleCurrentChange(val) {
      console.log(val)
      this.pageIndex = +val
      this.gender()
    },

    // 自定义序号
    indexMethod(index) {
      return (this.pageIndex - 1) * 10 + index + 1
    }
  }
}
</script>

<style lang="scss" scoped>
.userList {
  width: 100%;
  height: 64px;
  background-color: #fff;
  border-radius: 2px;
  .search {
    line-height: 64px;
    margin-bottom: 20px;
    .el-col {
      display: flex;
      justify-content: space-evenly;
      align-items: center;
      span {
        font-size: 14px;
        color: #606266;
      }
      .el-input {
        font-size: 14px;
        width: 206px;
      }
      .el-button {
        height: 36px;
        background-color: #5f84ff;
        border-color: #5f84ff;
      }
    }
  }
  .table {
    width: 100%;
    padding: 10px;
    background-color: #fff;
    border-radius: 2px;
    .el-button {
      font-size: 14px;
      border-radius: 4px;
      color: #fff;
      width: 80px !important;
      height: 36px;
      background: linear-gradient(135deg, #ff9743, #ff5e20) !important;
      border: none;
      margin-bottom: 10px;
    }
    .footer {
      width: 100%;
      display: flex;
      justify-content: space-between;
      height: 96px;
      align-items: center;
      span {
        margin-right: 10px;
        font-weight: 400;
        font-size: 16px !important;
        color: #dbdfe5 !important;
        display: inline-block;
        line-height: 28px;
      }
    }
  }
}
.el-pagination {
  display: inline-block;
  width: 200px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
::v-deep .el-pagination .btn-prev {
  display: block !important;
  width: 70px !important;
  height: 32px !important;
  background-color: #d5ddf8;
}
::v-deep .el-pagination .btn-next {
  display: block !important;
  width: 70px !important;
  height: 32px !important;
  background-color: #d5ddf8;
}
</style>
